package hu.issue.web;

import hu.issue.ejb.TaskService;
import hu.issue.ejb.UserService;
import hu.issue.framework.web.Constants;
import hu.issue.framework.web.controller.AbstractFormController;
import hu.issue.framework.web.controller.Inject;
import hu.issue.framework.web.controller.RequiredRole;
import hu.issue.framework.web.validation.ErrorMessages;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequiredRole(Constants.ROLE_PROJECTOWNER)
public class AssignToController extends AbstractFormController {
	@Inject
	private TaskService taskService;

	@Inject
	private UserService userService;

	@Override
	protected void buildModel(final HttpServletRequest request) throws Exception {
		final int taskId = Integer.parseInt(request.getParameter("taskId"));
		request.setAttribute("task", this.taskService.findTaskById(taskId));
		request.setAttribute("users", this.userService.getUsers());
	}

	@Override
	protected String handleSubmission(final HttpServletRequest request, final HttpServletResponse response, final ErrorMessages messages) throws Exception {
		final int taskId = Integer.parseInt(request.getParameter("taskId"));
		final String userName = request.getParameter("user");
		this.taskService.assignTo(taskId, userName);
		return "redirect:project.do?projectId=" + request.getParameter("projectId");
	}

}
